package com.xxx;

import java.util.ArrayList;
import java.util.Scanner;

public class ManagementFunction {

    ArrayList<Student> student = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    Student st = new Student();//当前对象
    String s; //接收键入数

    public void main(){

        while (true) {
            System.out.println("--------欢迎使用学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");

            System.out.println("请输入您的选择：");
            s = sc.next();
            switch (s){
                case "1":
                    addStudent();
                    break;
                case "2":
                    delete();
                    break;
                case "3":
                    modify();
                    break;
                case "4":
                    check();
                    System.out.println("输入任意值返回主菜单~~");
                    sc.next();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("没有该选项，请重新输入~~");
            }
        }
    }


    //添加功能
    public void addStudent(){

        while (true) {
            boolean a = true;
            Student stu = new Student();
            while (a) {
                System.out.println("请输入要添加的学生id：");
                s = sc.next();

                if (student.size()==0){
                    break;
                }
                for (int i = 0; i < student.size(); i++) {
                    if (student.get(i).getId().equals(s)){
                        a = true;
                        System.out.println("id已被占用，请重新输入~~");
                        break;
                    }
                    else {
                        a = false;
                    }
                }

            }
            stu.setId(s);
            System.out.println("请输入要添加的学生姓名：");
            s = sc.next();
            stu.setName(s);
            System.out.println("请输入要添加的学生年龄：");
            s = sc.next();
            stu.setAge(s);
            System.out.println("请输入要添加的学生家庭住址：");
            s = sc.next();
            stu.setAddress(s);
            student.add(stu);
            System.out.println("添加成功，是否继续添加？ y/n :");
            s = sc.next();
            switch (s){
                case "y":
                    break;
                default:
                    return;
            }
        }
    }

    //删除
    public void delete(){
        if(!check()){
            return;
        }
        System.out.println("请输入要删除的学生id：");
        s = sc.next();
        for (int i = 0; i < student.size(); i++) {
            if(student.get(i).getId().equals(s)){
                student.remove(i);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("id不存在。");

    }

    //修改
    public void modify(){
        if(!check()){
            return;
        }
        System.out.println("请输入要修改信息的学生id：");
        s = sc.next();
        for (int i = 0; i < student.size(); i++) {
            if(student.get(i).getId().equals(s)){
                System.out.println("请输入要修改后的学生姓名：");
                s = sc.next();
                student.get(i).setName(s);
                System.out.println("请输入要修改后的学生年龄：");
                s = sc.next();
                student.get(i).setAge(s);
                System.out.println("请输入要修改后的学生家庭住址：");
                s = sc.next();
                student.get(i).setAddress(s);
                System.out.println("修改成功");
                return;
            }
        }
        System.out.println("id不存在~~");

    }

    //查看
    public boolean check(){
        if(student.size()==0){
            System.out.println("当前无学生信息，请添加后操作~~");
            return false;
        }
        System.out.println("\tid\t\t\t姓名\t\t\t年龄\t\t\t家庭住址");

        for (int i = 0; i < student.size(); i++) {
            st = student.get(i);
            System.out.println(String.format("%-10s\t%-11.11s%-10s\t%s",st.getId(),st.getName(),st.getAge(),st.getAddress()));
        }
        return true;
    }

}
